package com.ywgt.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.Log;
import com.ywgt.system.pojo.LoginLog;
import com.ywgt.system.service.LogService;
import com.ywgt.system.service.LoginLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 登录日志表 前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@RestController
@Api(value = "登录日志模块",tags = "登录日志模块")
@RequestMapping("/system/loginLog")
public class LoginLogController {


    @Autowired
    private LoginLogService loginLogService;

    @ApiOperation(value = "查询日志",notes = "根据条件查询日志")
    @GetMapping("/findLoginLogList")
    public Result findLogList(@RequestParam(value = "pageNum",defaultValue = "1",required = false)Long pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "3",required = false)Long pageSize,
                              LoginLog loginLog){
        Page<LoginLog> logPage= loginLogService.findLoginLogList(pageNum,pageSize,loginLog);
        List<LoginLog> pageRecords = logPage.getRecords();
        long total = logPage.getTotal();
        return Result.ok().data("rows",pageRecords).data("total",total);
    }

    @ApiOperation(value = "删除登录日志",notes = "根据id删除登录日志")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Long id){
        if (loginLogService.removeById(id)){
            return Result.ok();
        }
        return Result.error();
    }

    @ApiOperation(value = "批量删除登录日志",notes = "批量删除登录日志")
    @DeleteMapping("/batchDelete/{ids}")
    public Result batchDelete(@PathVariable("ids")Long [] ids){
        List<Long> id = Arrays.asList(ids);
        if (loginLogService.removeByIds(id)){
            return Result.ok();
        }
        return Result.error();
    }


    @PostMapping("/loginReport")
    public Result loginReport(@RequestBody String username){
        List<Map<String,Object>> currentUser=loginLogService.getReport(username);
        List<Map<String, Object>> all = loginLogService.getReport(null);
        return Result.ok().data("me",currentUser).data("all",all);
    }
}

